﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;

using AtletIS.DomainModel.Entities.Main;
using AtletIS.Service.Services;
using AtletIS.UI.Commands;
using AtletIS.UI.Messaging;
using AtletIS.UI.ViewModels.Base;
using AtletIS.UI.Views;

namespace AtletIS.UI.ViewModels
{
    public class PressViewModel : MediatorViewModelBase<ViewModelMessages>
    {

        #region Private properties

        private bool _facingFront = true;
        private object _frontOutputViewModel;
        private object _backOutputViewModel;
        
        private Competition _activeCompetition;
        private DisciplineListViewModel _disciplineListViewModel;

        private bool _hasPages = true;

        #endregion

        #region Public properties

        public bool FacingFront
        {
            get { return _facingFront; }
            set
            {
                _facingFront = value;
                OnPropertyChanged("FacingFront");
            }
        }

        public object FrontOutputViewModel
        {
            get { return _frontOutputViewModel; }
            private set
            {
                _frontOutputViewModel = value;
                OnPropertyChanged("FrontOutputViewModel");
            }
        }

        public object BackOutputViewModel
        {
            get { return _backOutputViewModel; }
            private set
            {
                _backOutputViewModel = value;
                OnPropertyChanged("BackOutputViewModel");
            }
        }

        public Competition ActiveCompetition
        {
            get { return _activeCompetition; }
            set
            {
                _activeCompetition = value;
                OnPropertyChanged("ActiveCompetition");
            }
        }

        public DisciplineListViewModel DisciplineListViewModel
        {
            get 
            {
                if (_disciplineListViewModel == null)
                {
                    _disciplineListViewModel = new DisciplineListViewModel(this.ActiveCompetition);
                }
                return _disciplineListViewModel; 
            }
            set
            {
                _disciplineListViewModel = value;
                OnPropertyChanged("DisciplineListViewModel");
            }
        }

        public bool HasPages
        {
            get { return _hasPages; }
            set
            {
                _hasPages = value;
                OnPropertyChanged("HasPages");

                base.Mediator.NotifyColleagues(ViewModelMessages.HasPagesChanged, this.HasPages);
            }
        }
        
        public ICommand StartListCommand { get; set; }
        public ICommand ResultsCommand { get; set; }

        #endregion

        #region Constructor

        public PressViewModel()
        {
            this.StartListCommand = new RelayCommand(new Action<object>(ShowStartList));
            this.ResultsCommand = new RelayCommand(new Action<object>(ShowResults));
            
            if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                using (var service = new CompetitionService())
                {
                    this.ActiveCompetition = service.GetActive();
                    this.DisciplineListViewModel = new DisciplineListViewModel(this.ActiveCompetition);
                    this.FrontOutputViewModel = new StartListViewModel(this.DisciplineListViewModel.ActiveDisciplineList, this.HasPages);
                }
            }
        }

        #endregion

        #region Commands

        public void ShowStartList(object obj)
        {
            if (this.FacingFront)
            {
                this.BackOutputViewModel = new StartListViewModel(this.DisciplineListViewModel.ActiveDisciplineList, this.HasPages);
            }
            else
            {
                this.FrontOutputViewModel = new StartListViewModel(this.DisciplineListViewModel.ActiveDisciplineList, this.HasPages);
            }

            this.FacingFront = !this.FacingFront;
        }

        public void ShowResults(object obj)
        {
            if (this.FacingFront)
            {
                this.BackOutputViewModel = new ResultsViewModel();
            }
            else
            {
                this.FrontOutputViewModel = new ResultsViewModel();
            }

            this.FacingFront = !this.FacingFront;
        }

        #endregion

    }
}
